<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/7/10
 * Time: 15:03
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class NewInfo extends Model
{

    //消息表名
    protected $table = 'new_info';

    /**
     * titel 添加消息
     * @param $uid
     * @param $realname
     * @param $adjust_id
     * @return int
     * @throws \yii\db\Exception
     */
    public function addInfo($uid,$realname,$adjust_id,$type)
    {

        $create_time = date('Y-m-d H:i:s',time());
        $is_read = 1;

//        $adjust = $query->select(['item_id','fee_name','budget_total','buy_total'])->from('ifssc_php.atlas_budget')->where(['id'=>$adjust_id])->one();
//        //查询课题号
//        $project_info = $query->select(['project_no'])->from('ifssc_php.project_info')->where(['id'=>$adjust['item_id']])->one();
        //详情
        switch($type){
            case 1:
                    $title = $realname.'报销成功通知';
                break;
            case 2:
                    $title = $realname.'使用推荐课题报销成功通知';
                break;
            case 3:
                    $title = $realname.'科目调整报销成功通知';
                break;
            default:
                    $title = $realname.'报销成功通知';
                break;
        }
        $field = ['uid','title','create_time','adjust_id','is_read','type'];
        $batchData = [[$uid,$title,$create_time,$adjust_id,$is_read,$type]];
        $add = Yii::$app->db->createCommand()->batchInsert($this->table,$field,$batchData)->execute();

        return $add;

    }


    /**
     * title 消息列表
     * @param $id
     * @return array
     */
    public function fiveInfo($id)
    {
        $query = new \yii\db\Query();

        $newInfo = $query->select(['id','title','uid','create_time','adjust_id','is_read','type'])->from('ifssc_php.new_info')->orderBy('create_time DESC')->limit(5)->all();
        return $newInfo;


    }


    /**
     * title 全部消息列表
     * @param $id
     * @return array
     */
    public function selectInfo($id)
    {
        $query = new \yii\db\Query();

        $newInfo = $query->select(['id','title','uid','create_time','adjust_id','is_read','type'])->from('ifssc_php.new_info')->orderBy('create_time DESC')->all();
        return $newInfo;


    }

    /**
     * title 详情
     * @param $id
     * @return array|bool
     */
    public function detailsInfo($infoId)
    {

        $time = date('Y-m-d H:i:s',time());
        //修改已读
        $is_read = Yii::$app->db->createCommand()->update('ifssc_php.new_info', ['is_read'=>2,'update_time' =>$time], 'id = '.$infoId)->execute();
        if(!$is_read){
            return 2;
        }
        //获取adjust_id
        $query = new \yii\db\Query();
        $adjust_id = $query->select(['adjust_id','type'])->from('ifssc_php.new_info')->where(['id'=>$infoId])->one();
        //联表
        $newInfo = $query
            ->select('a.title,a.is_read,a.create_time,b.price,b.few_id,b.add_id')
            ->from('ifssc_php.new_info AS a')
            ->rightJoin('ifssc_php.budget_adjust AS b', 'b.id = ' . $adjust_id['adjust_id'])
            ->where(['a.id' => $infoId])
            ->one();
        $newInfo['type'] = $adjust_id['type'];
        //返回余额
        $query = new \yii\db\Query();
        $left_add = $query->select(['item_id', 'fee_name', 'budget_total', 'buy_total'])->from('ifssc_php.atlas_budget')->where(['id' => $newInfo['few_id']])->one();

        $right_few = $query->select(['fee_name', 'budget_total', 'buy_total'])->from('ifssc_php.atlas_budget')->where(['id' => $newInfo['add_id']])->one();

        //查询课题号
        $project_info = $query->select(['project_no'])->from('ifssc_php.project_info')->where(['id' => $left_add['item_id']])->one();
        $newInfo['project_no'] = $project_info['project_no'];

        //1正常报销  2调整余额
        if($adjust_id['type'] == 1 || ($adjust_id['type'] == 2)){
            $newInfo['buy_total'] = sprintf("%.3f", round($left_add['buy_total'] / 10000 * 100, 1) / 100);;
            $newInfo['budget_total'] = sprintf("%.3f", round(($left_add['budget_total'] - $newInfo['price']) / 10000 * 100, 1) / 100);

            $newInfo['name_add'] = $left_add['fee_name'];

            $newInfo['new_price'] = sprintf("%.3f", round($newInfo['price'] / 10000 * 100, 1) / 100);

        }else if($adjust_id['type'] == 3) {


            $newInfo['buy_total_add'] = sprintf("%.3f", round($left_add['buy_total'] / 10000 * 100, 1) / 100);;
            $newInfo['budget_total_add'] = sprintf("%.3f", round(($left_add['budget_total'] - $newInfo['price']) / 10000 * 100, 1) / 100);
            $newInfo['name_add'] = $left_add['fee_name'];

            $newInfo['buy_total_few'] = sprintf("%.3f", round($right_few['buy_total'] / 10000 * 100, 1) / 100);
            $newInfo['budget_total_few'] = sprintf("%.3f", round($right_few['budget_total'] / 10000 * 100, 1) / 100);
            $newInfo['name_few'] = $right_few['fee_name'];


            $newInfo['new_price'] = sprintf("%.3f", round($newInfo['price'] / 10000 * 100, 1) / 100);
        }

        return $newInfo;


    }


}